package com.ghp.service;

import com.ghp.mapper.UserMapper;
import com.ghp.pojo.User;
import com.ghp.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * @author ghp
 */
public class UserServiceImp implements UserService{
    //1、获取SqlSessionFactory对象
    SqlSessionFactory sqlSF = SqlSessionFactoryUtil.getSqlSF();

    /**
     * 判断密码是否正确
     * @return null表示密码不正确
     */
    @Override
    public User select(User user) {
        //2、获取SqlSession对象
        SqlSession sqlS = sqlSF.openSession();
        //3、获取Mapper接口对象
        UserMapper mapper = sqlS.getMapper(UserMapper.class);
        //4、执行SQL
        User u = mapper.select(user);
        return u;
    }

    /**
     * 判断用户名是否存在
     * @param name
     * @return
     */
    @Override
    public User selectByName(String name) {
        //2、获取SqlSession对象
        SqlSession sqlS = sqlSF.openSession();
        //3、获取Mapper接口对象
        UserMapper mapper = sqlS.getMapper(UserMapper.class);
        //4、执行SQL
        User user = mapper.selectByName(name);
        return user != null ? user : null;
    }

    /**
     * 添加用户数据
     * @param user
     */
    @Override
    public void add(User user) {
        //2、获取SqlSession对象
        SqlSession sqlS = sqlSF.openSession(true);
        //3、获取mapper接口对象
        UserMapper mapper = sqlS.getMapper(UserMapper.class);
        //4、执行SQL
        mapper.add(user);
    }


}
